package com.highspeed.bdk.controller.base;

import com.bianmaba.spring.data.jpa.domain.PageableRequest;
import com.bianmaba.spring.data.jpa.domain.PageableResult;
import com.bianmaba.spring.data.jpa.domain.Result;
import com.bianmaba.spring.mvc.controller.ControllerSupports;
import com.bianmaba.spring.mvc.method.support.annotations.EntityParam;
import com.bianmaba.spring.mvc.method.support.annotations.JSON;
import com.bianmaba.spring.mvc.method.support.annotations.MapParam;
import com.highspeed.bdk.service.base.IBdkBaseService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.Serializable;
import java.util.LinkedHashMap;
import java.util.List;

public abstract class BdkReadController<T, ID extends Serializable> extends ControllerSupports {

    protected IBdkBaseService<T, ID> getService() {
        return null;
    }


    @JSON()
    @ApiOperation(value = "根据数据ID查询单个数据", notes = "如果指定ID的数据不存在，则返回NULL")
    @ApiImplicitParam(name = "id", value = "查询", dataType = "int", paramType = "query")
    @RequestMapping(value = "/get_by_id", method = RequestMethod.POST)
    public Result getById(ID id) {
        Result result = new Result();
        try {
            T data = getService().findOne(id);
            result.setSuccess(true);
            result.setResult("数据获取成功！");
            result.setData(data);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setResult("获取数据出现异常：" + e.getLocalizedMessage());
            result.setException(e);
        }
        return result;
    }

    @JSON()
    @ApiOperation(value = "获取所有数据", notes = "返回数据中的data为一个数据列表")
    @RequestMapping(value = "/all", method = RequestMethod.POST)
    public Result all() {
        List<T> data = getService().all();
        Result result = new Result(true, "获取所有数据成功！", data);
        return result;
    }

    @JSON()

    @ApiOperation(value = "综合查询接口", notes = "返回数据中的data为一个数据列表<br/>此函数有2组参数，第一组是查询参数，以params开头，第二组为分页参数，以pageable开头<br/>参数示例如下（支持form data或queryString），<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;params.username_eq:maomao(表示username等于maomao,eq表示等于)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;params.age_gt:10(表示age大于10，gt表示大于，ge为大于等于，le为小于等于，lt为小于)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;params.xxx_like:val%(表示用某个属性做LIKE比较,比较值为val%，另外支持notlike)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;pageable.page:0(当前页)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;pageable.start:0(起始记录)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;pageable.size:25(共取多少条数据)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;pageable.sort:【{\"property\":\"cdt\",\"direction\":\"ASC\"}】(用cdt降序排序，支持多组排序)")
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public PageableResult query(@MapParam LinkedHashMap<String, Object> params, @EntityParam PageableRequest pageable) {
        PageableResult result = new PageableResult(null);
        Page<T> data = getService().query(params, pageable);
        result.setSuccess(true);
        result.setResult("获取所有数据成功！");
        result.setData(data);
        return result;
    }

    @JSON()
    @ApiOperation(value = "去重复的综合查询接口", notes = "返回数据中的data为一个数据列表<br/>此函数有2组参数，第一组是查询参数，以params开头，第二组为分页参数，以pageable开头<br/>参数示例如下（支持form data或queryString），<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;params.username_eq:maomao(表示username等于maomao,eq表示等于)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;params.age_gt:10(表示age大于10，gt表示大于，ge为大于等于，le为小于等于，lt为小于)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;params.xxx_like:val%(表示用某个属性做LIKE比较,比较值为val%，另外支持notlike)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;pageable.page:0(当前页)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;pageable.start:0(起始记录)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;pageable.size:25(共取多少条数据)<br/>\n" +
            "&nbsp;&nbsp;&nbsp;&nbsp;pageable.sort:【{\"property\":\"cdt\",\"direction\":\"ASC\"}】(用cdt降序排序，支持多组排序)")
    @RequestMapping(value = "/query_distinct", method = RequestMethod.POST)
    public PageableResult queryDistinct(@MapParam LinkedHashMap<String, Object> params, @EntityParam PageableRequest pageable) {
        PageableResult result = new PageableResult(null);
        Page<T> data = getService().queryDistinct(params, pageable);
        result.setSuccess(true);
        result.setResult("获取所有数据成功！");
        result.setData(data);
        return result;
    }
}
